package com.imooc.o2o.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.imooc.o2o.cache.JedisUtil;
import com.imooc.o2o.dao.ShopCategoryDao;
import com.imooc.o2o.entity.ShopCategory;
import com.imooc.o2o.exception.ShopCategoryOperationException;
import com.imooc.o2o.service.ShopCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Knight on 2018/7/3.
 */
@Service
public class ShopCategorySerivceImpl implements ShopCategoryService {
    @Autowired
    private JedisUtil.Strings jedisStrings;
    @Autowired
    private JedisUtil.Keys jedisKeys;
    @Autowired
    private ShopCategoryDao shopCategoryDao;

    private static Logger logger= LoggerFactory.getLogger(ShopCategorySerivceImpl.class);

    private static String SCLISTKEY = "shopcategorylist";
    @Override
    public List<ShopCategory> getShopCategoryList(ShopCategory shopCategoryCondition) {
        String key = SCLISTKEY ;
        List<ShopCategory> shopCategoryList = null;
        ObjectMapper mapper = new ObjectMapper();
        if(shopCategoryCondition==null){
            //若查询条件为空,则列出所有首页大类,即parentId为空的店铺类别
            key=key+"_allfirstlevel";
        }else if(shopCategoryCondition!=null && shopCategoryCondition.getParent()!=null){
            //若parentId为非空,则列出该parentId下的所有子类别
            key=key+"_parent"+shopCategoryCondition.getParent().getShopCategoryId();
        }else if(shopCategoryCondition!=null){
            //列出所有子类别,不管其属于哪个类,都列出来
            key=key+"_allsecondlevel";
        }
        //判断key是否存在
        if (!jedisKeys.exists(key)) {
//            ShopCategory shopCategoryCondition = new ShopCategory();
//            shopCategoryCondition.setParentId(parentId);
            shopCategoryList = shopCategoryDao.queryShopCategory(shopCategoryCondition);
            String jsonString = null;
            try {
                jsonString = mapper.writeValueAsString(shopCategoryList);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
                throw new ShopCategoryOperationException(e.getMessage());
            }
            jedisStrings.set(key, jsonString);
        } else {
            String jsonString = jedisStrings.get(key);
            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class,ShopCategory.class);
            try {
                shopCategoryList = mapper.readValue(jsonString, javaType);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
                throw new ShopCategoryOperationException(e.getMessage());
            }
        }
        return shopCategoryList;
    }


}
